<?php

namespace App\Api\Controllers;

/* use App\Http\Controllers\Controller; */
use Illuminate\Http\Request;
use App\Api\Models\QuotationGoodsModel;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Api\BaseController;
class QuotationGoodsController extends BaseController
{
    
    public function add(Request $request){
        
       $data = $request->all();       
       return $this->success(['goods_id'=>QuotationGoodsModel::insertGetId($data)]);
    }

    public function find(Request $request){
        
        $info = QuotationGoodsModel::where(['id'=>$request->input('goods_id')])->first();
        
        return $this->success($info);
    }
    
    public function goodsList(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $condition['user_id'] = $this->getUserId();
        $condition['main_id'] = $this->getMainUserId();
        $list = QuotationGoodsModel::where($condition)
        ->orderBy('id','desc')
        ->paginate($data['pageSize'],['*'], '',$data['currentPage'])
        ->toArray();
        
        return $this->success($list);
    }
    
    public function delete(Request $request){
        
        $condition['user_id'] = $this->getUserId();
        $condition['main_id'] = $this->getMainUserId();
        $condition['id']      = $request->input('goods_id');
        
        $bool = QuotationGoodsModel::where($condition)->delete();
        
        return $this->success();
    }
    
    public function upd(Request $request){
        
        $data = $request->all();
        $condition['user_id'] = $this->getUserId();
        $condition['main_id'] = $this->getMainUserId();
        $condition['id']      = $data['goods_id'];
        unset($data['goods_id']);
        QuotationGoodsModel::where($condition)->update($data);
        return $this->success();
    
    }
    
    
}
